using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Configuration;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;


using IDAL;
using Entities;

namespace Bll
{
    public class ProductBll
    {
        public IList<ProductInfo> GetAll()
        {
            IProductDAO iPro = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return iPro.GetAll();
        }

        public ProductInfo GetByID(string productID)
        {
            IProductDAO iPro = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return iPro.GetByID(productID);
        }

        public int CountProduct(string categoryID)
        {
            IProductDAO iPro = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return iPro.CountProduct(categoryID);
        }

        public IList<ProductInfo> GetOnlyName()
        {
            IProductDAO iPro = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return iPro.GetOnlyName(); 
        }

        public IList<ProductInfo> GetTop3Product()
        {
            IProductDAO iPro = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return iPro.GetTop3Product();
        }

        public IList<ProductInfo> GetRandom6Product()
        {
            IProductDAO iPro = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return iPro.GetRandom6Product();
        }

        public IList<ProductInfo> GetByCategoryID(string categoryID)
        {
            IProductDAO iPro = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return iPro.GetByCategoryID(categoryID);
        }

        public bool Insert(ProductInfo pro)
        {
            IProductDAO iPro = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return iPro.Insert(pro);
        }

        public bool Update(ProductInfo pro)
        {
            IProductDAO iPro = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return iPro.Update(pro);
        }

        public bool UpdatePicture(string imagePath, string productID)
        {
            IProductDAO iPro = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return iPro.UpdatePicture(imagePath, productID);
        }

        public bool UpdateUnitOnOrder(string unitOnOrder, string productID)
        {
            IProductDAO iPro = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return iPro.UpdateUnitOnOrder(unitOnOrder, productID);
        }

        public bool UpdateRating(string productID)
        {
            IProductDAO iPro = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return iPro.UpdateRating(productID);
        }

        public bool Delete(ProductInfo pro)
        {
            IProductDAO iPro = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return iPro.Delete(pro);
        }

        public IList<ProductInfo> GetProductByAllCategoryAndSearchkey(string searchkey)
        {
            IProductDAO dal = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return dal.GetProductByAllCategoryAndSearchkey(searchkey);
        }

        public string GetCountProductByAllCategoryAndSearchkey(string searchkey)
        {
            IProductDAO dal = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return dal.GetCountProductByAllCategoryAndSearchkey(searchkey);
        }

        public IList<ProductInfo> GetProductByAllCategoryAndPrice(string pricefrom, string priceto)
        {
            IProductDAO dal = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return dal.GetProductByAllCategoryAndPrice(pricefrom, priceto);
        }

        public string GetCountProductByAllCategoryAndPrice(string pricefrom, string priceto)
        {
            IProductDAO dal = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return dal.GetCountProductByAllCategoryAndPrice(pricefrom, priceto);
        }

        public IList<ProductInfo> GetProductByCategoryAndPrice(string pricefrom, string priceto, int categoryID)
        {
            IProductDAO dal = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return dal.GetProductByCategoryAndPrice(pricefrom, priceto, categoryID);
        }

        public string GetCountProductByCategoryAndPrice(string pricefrom, string priceto, int categoryID)
        {
            IProductDAO dal = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return dal.GetCountProductByCategoryAndPrice(pricefrom, priceto, categoryID);
        }


        public IList<ProductInfo> GetProductByCategoryAndSearchkey(string searchkey, int categoryID)
        {
            IProductDAO dal = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return dal.GetProductByCategoryAndSearchkey(searchkey, categoryID);
        }

        public string GetCountProductByCategoryAndSearchkey(string searchkey, int categoryID)
        {
            IProductDAO dal = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return dal.GetCountProductByCategoryAndSearchkey(searchkey, categoryID);
        }


        public IList<ProductInfo> GetProductByAllCategoryAndPriceAndSearchkey(string searchkey, string pricefrom, string priceto)
        {
            IProductDAO dal = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return dal.GetProductByAllCategoryAndPriceAndSearchkey(searchkey, pricefrom, priceto);
        }

        public string GetCountProductByAllCategoryAndPriceAndSearchkey(string searchkey, string pricefrom, string priceto)
        {
            IProductDAO dal = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return dal.GetCountProductByAllCategoryAndPriceAndSearchkey(searchkey, pricefrom, priceto);
        }

        public IList<ProductInfo> GetProductByCategoryAndPriceAndSearchkey(string searchkey, string pricefrom, string priceto, int categoryID)
        {
            IProductDAO dal = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return dal.GetProductByCategoryAndPriceAndSearchkey(searchkey, pricefrom, priceto, categoryID);
        }

        public string GetCountProductByCategoryAndPriceAndSearchkey(string searchkey, string pricefrom, string priceto, int categoryID)
        {
            IProductDAO dal = (IProductDAO)AssemblyLoader.Load("ProductDAO");
            return dal.GetCountProductByCategoryAndPriceAndSearchkey(searchkey, pricefrom, priceto, categoryID);
        }


        public void SaveUploadImage(HttpPostedFile myPostedFile, string uploadedFileName, int targetSize)
        {
            int fileLength = myPostedFile.ContentLength;
            byte[] bytes = new byte[fileLength];
            myPostedFile.InputStream.Read(bytes, 0, fileLength);
            //ResizeImage takes two parameters : byte[] and the IMAGE SIZE !!
            byte[] resized = ResizeImage(bytes, targetSize);
            FileStream fs = new FileStream(uploadedFileName, FileMode.Create);
            fs.Write(resized, 0, resized.Length);
            fs.Close();
        }

        public byte[] ResizeImage(byte[] imageFile, int targetSize)
        {
            System.Drawing.Image original = System.Drawing.Image.FromStream(new MemoryStream(imageFile));
            int targetH, targetW;
            if (original.Height > original.Width)
            {
                targetH = targetSize;
                targetW = (int)(original.Width * ((float)targetSize / (float)original.Height));
            }
            else
            {
                targetW = targetSize;
                targetH = (int)(original.Height * ((float)targetSize / (float)original.Width));
            }
            System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(new MemoryStream(imageFile));
            
            Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(72, 72);
            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
            grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
            grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);
            
            MemoryStream mm = new MemoryStream();
            bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
            original.Dispose();
            imgPhoto.Dispose();
            bmPhoto.Dispose();
            grPhoto.Dispose();
            return mm.GetBuffer();
        }
    }
}
